package cn.itcast.controller;


import cn.itcast.dao.SkuMapper;
import cn.itcast.pojo.Sku;
import com.alibaba.fastjson.JSON;

import com.github.pagehelper.PageHelper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.awt.print.Pageable;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@SuppressWarnings("all")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-dao.xml")
public class Test1 {
    /**
     * 新增和修改数据
     * @param args
     * @throws IOException
     */
    @Autowired
    private SkuMapper skuMapper;



    /**
     * 批量新增和修改数据
     * @throws IOException
     */
    @Test
    public void Test2() throws IOException {
        //1、连接rest接口
        HttpHost http = new HttpHost("127.0.0.1",9200,"http");
        RestClientBuilder builder = RestClient.builder(http);//rest构建器
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder); //高级客户端对象


        //2、封装请求对象
        BulkRequest bulkRequest = new BulkRequest();


            PageHelper.startPage(1,1000);//分页助手
            List<Sku> skuList = skuMapper.selectAll();
            System.out.println("skuList.size() = " + skuList.size());
            for (Sku sku : skuList) {
                Map skuMap = new HashMap();
                IndexRequest indexRequest = new IndexRequest("sku", "doc",sku.getId());
                skuMap.put("name",sku.getName());
                skuMap.put("brandName",sku.getBrandName());
                skuMap.put("categoryName",sku.getCategoryName());
                skuMap.put("spuId",sku.getSpuId());
                skuMap.put("image",sku.getImage());
                skuMap.put("price",sku.getPrice());
                skuMap.put("createTime",sku.getCreateTime());
                skuMap.put("saleNum",sku.getSaleNum());
                skuMap.put("commentNum",sku.getCommentNum());

                String spec = sku.getSpec();
                Map map=  (Map)JSON.parseObject(spec);
                skuMap.put("spec",map);
                indexRequest.source(skuMap);
                bulkRequest.add(indexRequest);//可以多次添加

            }
            //3、获取响应结果
            BulkResponse responses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
            int status = responses.status().getStatus();
            System.out.println(status);
        restHighLevelClient.close();

    }




}
